home *** CD-ROM | disk | FTP | other *** search
- global gPlayer, gpCollisionSprites
-
- on p_Init
- gpCollisionSprites = []
- end
-
- on p_RegisterObject theObject
- add(gpCollisionSprites, theObject)
- return count(gpCollisionSprites)
- end
-
- on p_UpdateObject theSlot, newloc, newVel
- gpCollisionSprites[theSlot][1] = newloc
- gpCollisionSprites[theSlot][2] = newVel
- end
-
- on p_Update
- repeat with i = 1 to count(gpCollisionSprites) - 1
- repeat with j = i + 1 to count(gpCollisionSprites)
- p_checkForCollision(i, j)
- end repeat
- end repeat
- end
-
- on p_GetDistance loc1, loc2
- if voidp(loc1) or voidp(loc2) then
- return 0
- end if
- diff = loc2 - loc1
- if diff = point(0, 0) then
- return 0
- else
- return sqrt(power(diff[1], 2) + power(diff[2], 2))
- end if
- end
-
- on p_DotProduct vec1, vec2
- return (vec1[1] * vec2[1]) + (vec1[2] * vec2[2])
- end
-
- on p_checkForCollision sprite1, sprite2
- loc1 = gpCollisionSprites[sprite1][1]
- rad1 = gpCollisionSprites[sprite1][3]
- loc2 = gpCollisionSprites[sprite2][1]
- rad2 = gpCollisionSprites[sprite2][3]
- dist = p_GetDistance(loc1, loc2)
- if dist = 0 then
- return
- end if
- if dist <= (rad1 + rad2) then
- p_handleCollision(sprite1, sprite2)
- end if
- end
-
- on p_handleCollision sprite1, sprite2
- m1 = gpCollisionSprites[sprite1][4]
- v1i = gpCollisionSprites[sprite1][2]
- loc1 = gpCollisionSprites[sprite1][1]
- m2 = gpCollisionSprites[sprite2][4]
- v2i = gpCollisionSprites[sprite2][2]
- loc2 = gpCollisionSprites[sprite2][1]
- loi = loc2 - loc1
- loiLength = p_GetDistance(point(0, 0), loi)
- n = loi / loiLength
- t = point(-n[2], n[1])
- v1in = p_DotProduct(v1i, n)
- v1it = p_DotProduct(v1i, t)
- v2in = p_DotProduct(v2i, n)
- v2it = p_DotProduct(v2i, t)
- v1fn = ((2 * m2 * v2in) + (v1in * (m1 - m2))) / (m1 + m2)
- v2fn = ((2 * m1 * v1in) + (v2in * (m2 - m1))) / (m1 + m2)
- x1f = (n[1] * v1fn) + (t[1] * v1it)
- y1f = (n[2] * v1fn) + (t[2] * v1it)
- v1f = point(x1f, y1f)
- x2f = (n[1] * v2fn) + (t[1] * v2it)
- y2f = (n[2] * v2fn) + (t[2] * v2it)
- v2f = point(x2f, y2f)
- if gpCollisionSprites[sprite1][5] = gPlayer.pMySpriteNum then
- gPlayer.crash(v1f)
- else
- e_Crash(gpCollisionSprites[sprite1][5], v1f)
- end if
- if gpCollisionSprites[sprite2][5] = gPlayer.pMySpriteNum then
- gPlayer.crash(v2f)
- else
- e_Crash(gpCollisionSprites[sprite2][5], v2f)
- end if
- if sound(2).isBusy() = 0 then
- puppetSound(2, "Crash")
- end if
- end
-